<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 问卷
 * @author Administrator
 *
 */
class Wj extends MY_Controller{

	public function __construct(){
		parent::__construct();
	}
	
// 	public function show(){
		
// 		$this->load->library('Mongo_db_n');
// 		$page = $this->input->get('page');
// 		$page = empty($page)?1:$page;
// 		$where = array();
// 		$size = 2;
// 		$count = $this->mongo_db_n->getCount('question');
// 		$skip = ($page-1)*$size;
// 		$questionList = $this->mongo_db_n->getAll('question',$where,$size,$skip);
// 		var_dump($questionList);die;
		
// // 		$questionList = array_merge($array1)
// 		$answerList = $this->mongo_db_n->getAll('answer');
// 		$newAnswerList = $this->new_answer_list($answerList,$questionList);
// 		var_dump($newAnswerList);
		
// 	}
	
// 	public function new_answer_list($answerList,$questionList){
		
// 		if (empty($answerList) || empty($questionList) ){
// 			return null;
// 		}
		
// 		//将答案id与答案名称关联
// 		$res = array();//最终返回数组
// 		foreach ($answerList as $k=>$v){
// 			if (!empty($v['answer'])){
// 				//$q_key 问题id $a_val 选项id值
// 				$resTemp = array();//用于存储整理后的答案
				
// 				foreach ($v['answer'] as $q_key=>$a_val){
// 					$qkey = str_replace('q', '', $q_key);
// 					$qkey = (int)($qkey-1);
// 					//复合选项有多个值
// 					if (is_array($a_val)){
// 						foreach ($a_val as $kk=>$vv){
// 							//选项值不是数字，则不用找其线相关联名称
// 							if (!is_numeric($vv)){
// 								continue;
// 							}
// 							$aKey = 'a_'.$vv;
// 							$a_name = $questionList[$qkey]['answer'][$aKey]['name'];
// 							$temp=array();
// 							$temp['id']=$vv;
// 							$temp['name']=$a_name;
// 							$resTemp[$q_key][] = $temp;
// 						}
// 					}else {
// 						if (is_numeric($a_val)){
// 							$aKey = 'a_'.$a_val;
// 							$a_name = $questionList[$qkey]['answer'][$aKey]['name'];
// 							$temp=array();
// 							$temp['id']=$a_val;
// 							$temp['name']=$a_name;
// 							$resTemp[$q_key][] = $temp;
// 						}
// 					}
// 				}
				
// 				$v['answer'] = $resTemp;
// 				$res[] = $v;
// 			}
// 		}
// 		return $res;	
		
// 	}
	
	/**
	 * 问卷调查
	 */
	public function index(){
		
		$post = $this->input->post(null,true);
		$this->load->library('Mongo_db_n');
		$user = parent::getLoginUser(false);
		if ( empty($post) ){
			$check = 0;
			if ( !empty($user) ){
				$checkExits = $this->mongo_db_n->getOne('answer',array('uid'=>$user->id));
				if (!empty($checkExits)){
					$check=1;
				}
			}else{
				$check=2;
			}
			
			$questionList = $this->mongo_db_n->getAll('question');
			$temp['title'] = '湖南荣钧直饮水APP问卷调查';
			$temp['exits'] = $check;
			$temp['list'] = $questionList;
			$this->load->view('wj/index',$temp);
		}else{
			if ( empty($user) ){
				$this->apiOutput->error('登录已失效请重新登录！');
			}
			$checkExits = $this->mongo_db_n->getOne('answer',array('uid'=>$user->id));
			if ( !empty($checkExits) ){
				$this->apiOutput->msg('您已完成此次问卷调查，非常感谢您的参与!');
			}
			$data['id']=0;
			$data['uid']=$user->id;
			$data['phone']=!empty($user->phone)?$user->phone:0;
			$data['create_time'] = time();
			//对post数据进行整理
			$newAnswerList = $this->new_answer_post($post);
			$data['answer']=$newAnswerList;
			
			//插入问题答案
			$aid = $this->mongo_db_n->mid('aid', 'idlist');//问题id
			$data['id']=$aid;
			$res = $this->mongo_db_n->toInsert('answer',$data);
			if ( !empty($res['err']) ){
				$this->apiOutput->msg('添加失败请重试！');
			}
			//更新问题答案数据
			$this->update_question_count($newAnswerList);
			$this->apiOutput->msg('添加成功');
		}
		
	}
	
	/**
	 * 更新回答总数
	 * @param unknown_type $newAnswerList
	 */
	protected function update_question_count($newAnswerList){
		
		foreach ($newAnswerList as $k=>$v){
			//问题id
			$qid = (int)str_replace('q', '', $k);
			//判断是否更新
			if (is_array($v)){
				if (!empty($v)){
					foreach ($v as $kk=>$vv){
						$qData=array();
						$where=array('id'=>$qid);
						if (!empty($vv['id'])){
							//复选框有附带内容
							$incKey = "answer.a_{$vv['id']}.a_count";
						}else{
							$incKey = "answer.a_{$vv}.a_count";
						}
						$qData[$incKey]=1;
						$this->mongo_db_n->toIncUpdate('question',$where,$qData);//多个更新
					}
				}
			}else{
				if (!empty($v) && is_numeric($v)){
					$qData=array();
					$where=array('id'=>$qid);
					$incKey = "answer.a_{$v}.a_count";
					$qData[$incKey]=1;
					//跟新总数
					$res = $this->mongo_db_n->toIncUpdate('question',$where,$qData);//单个更新
				}
			}
		}
		
	}
	
	/**
	 * 整理调查表
	 * @param unknown_type $post
	 * @return NULL
	 */
	protected function new_answer_post($post){
		
		if (empty($post) || !is_array($post)){
			return null;
		}
		$res = array();
		foreach ($post as $k=>$v){
			if ( is_array($v) ){
				foreach ($v as $kk=>$vv){
					if (!is_numeric($vv) && !is_null(json_decode($vv))){
						$arr = json_decode($vv,true);
						$v[$kk]=$arr;
					}
				}
			}
			$res[$k]=$v;
		}
		return $res;
		
	}
	
	
	/**
	 * 手动添加问卷(临时数据)
	 */
	public function import(){
		
		$this->load->library('Mongo_db_n');
// 		$res = $this->mongo_db_n->getAll('answer');
// 		var_dump($res);die;
		$yx = $this->input->get('yx',true);
		if (!empty($yx)&&$yx=='yx770'){
			$insert = true;
		}else{
			$insert = false;
		}
		$data=array(
				array(
				'id'=>0,
				'title'=>'你的性别是',
				'answer'=>array(
						'a_1'=>array('a_id'=>1,'name'=>'男','a_count'=>0),
						'a_2'=>array('a_id'=>2,'name'=>'女 ','a_count'=>0),
				),
				'qsort'=>0,
				'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'你的年龄段是',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'18岁或以下','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'19岁到27岁','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'28岁或以上','a_count'=>0),
						),
						'qsort'=>0,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'你喜欢喝什么饮品？',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'水','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'茶','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'牛奶or咖啡','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'碳酸饮料','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'其他','a_count'=>0),
						),
						'qsort'=>0,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'你平均一天饮用次数是？',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'五次以上','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'三次到五次','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'一两次','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'无','a_count'=>0),
						),
						'qsort'=>0,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'你饮用时，重点考虑的因素有？(多选，最多选五项)',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'个人承受价格','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'饮用方式','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'购买方式','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'饮品口感','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'饮品品牌','a_count'=>0),
								'a_6'=>array('a_id'=>6,'name'=>'支付方式','a_count'=>0),
								'a_7'=>array('a_id'=>7,'name'=>'包装外观','a_count'=>0),
						),
						'qsort'=>1,
						'qlimit'=>5
				),
				array(
						'id'=>0,
						'title'=>'你曾经用过、听过或正在使用的APP有？(多选)',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'微信、QQ (类似聊天软件)','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'淘宝、京东 (类似购物平台)','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'优酷、爱奇艺(类似视频APP)','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'荣钧直饮水','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'斗鱼、熊猫(类似直播软件)','a_count'=>0),
								'a_6'=>array('a_id'=>6,'name'=>'起点(类似阅读软件)','a_count'=>0),
								'a_7'=>array('a_id'=>7,'name'=>'酷我(类似音乐软件) ','a_count'=>0),
								'a_8'=>array('a_id'=>8,'name'=>'手游','a_count'=>0),
								'a_9'=>array('a_id'=>9,'name'=>'其他','a_count'=>0),
						),
						'qsort'=>1,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'你APP的使用情况是？',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'经常使用','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'偶尔使用','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'从未使用过','a_count'=>0),
						),
						'qsort'=>0,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'使用和下载APP的途径来源是？(多选，最多选三项)',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'亲人朋友同学','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'APP公司','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'报纸等纸质媒体','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'电视等电子媒体','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'斗鱼、熊猫(类似直播软件)','a_count'=>0),
								'a_6'=>array('a_id'=>6,'name'=>'网站或其它APP推荐','a_count'=>0),
								'a_7'=>array('a_id'=>7,'name'=>'其他','a_count'=>0),
						),
						'qsort'=>1,
						'qlimit'=>3
				),
				array(
						'id'=>0,
						'title'=>'你最常从APP获取哪些方面的信息？(多选，最多选三项)',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'交通','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'住宿','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'景点','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'美食','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'新闻','a_count'=>0),
								'a_6'=>array('a_id'=>6,'name'=>'娱乐','a_count'=>0),
								'a_7'=>array('a_id'=>7,'name'=>'购物','a_count'=>0),
								'a_8'=>array('a_id'=>8,'name'=>'音乐','a_count'=>0),
								'a_9'=>array('a_id'=>9,'name'=>'阅读','a_count'=>0),
								'a_10'=>array('a_id'=>10,'name'=>'目的地特色活动','a_count'=>0),
								'a_11'=>array('a_id'=>11,'name'=>'旅游攻略','a_count'=>0),
								'a_12'=>array('a_id'=>12,'name'=>'周边服务','a_count'=>0),
								'a_13'=>array('a_id'=>13,'name'=>'其他','a_count'=>0),
						),
						'qsort'=>1,
						'qlimit'=>3
				),
				array(
						'id'=>0,
						'title'=>'希望APP满足你的哪些需求？(多选)',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'交通工具班次查询','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'酒店与票务预订','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'电脑，数码DIY','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'各种达人攻略','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'新闻，娱乐，游戏资讯','a_count'=>0),
								'a_6'=>array('a_id'=>6,'name'=>'语音导航','a_count'=>0),
								'a_7'=>array('a_id'=>7,'name'=>'美食，快餐','a_count'=>0),
								'a_8'=>array('a_id'=>8,'name'=>'工作推荐','a_count'=>0),
								'a_9'=>array('a_id'=>9,'name'=>'跑腿服务','a_count'=>0),
								'a_10'=>array('a_id'=>10,'name'=>'二手买卖','a_count'=>0),
								'a_11'=>array('a_id'=>11,'name'=>'交友','a_count'=>0),
								'a_12'=>array('a_id'=>12,'name'=>'健康就医','a_count'=>0),
								'a_13'=>array('a_id'=>13,'name'=>'团购优惠','a_count'=>0),
								'a_14'=>array('a_id'=>14,'name'=>'直播 ','a_count'=>0),
								'a_15'=>array('a_id'=>15,'name'=>'阅读，鸡汤，美文，音乐','a_count'=>0),
								'a_16'=>array('a_id'=>16,'name'=>'视频，自拍','a_count'=>0),
								'a_17'=>array('a_id'=>17,'name'=>'其他','a_count'=>0),
						),
						'qsort'=>1,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'我公司新开发的直饮水APP你希望满足你哪些需求？(多选)',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'交通工具班次查询','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'酒店与票务预订','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'交友推荐','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'周边美食','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'周边团购','a_count'=>0),
								'a_6'=>array('a_id'=>6,'name'=>'工作兼职推荐','a_count'=>0),
								'a_7'=>array('a_id'=>7,'name'=>'校园跑腿服务','a_count'=>0),
								'a_8'=>array('a_id'=>8,'name'=>'正规小额贷款','a_count'=>0),
								'a_9'=>array('a_id'=>9,'name'=>'DIY各种达人分享','a_count'=>0),
								'a_10'=>array('a_id'=>10,'name'=>'各种充值服务','a_count'=>0),
								'a_11'=>array('a_id'=>11,'name'=>'二手物品买卖，交换','a_count'=>0),
								'a_12'=>array('a_id'=>12,'name'=>'直播，视频，自拍','a_count'=>0),
								'a_13'=>array('a_id'=>13,'name'=>'阅读，音乐，美文','a_count'=>0),
								'a_14'=>array('a_id'=>14,'name'=>'打车服务','a_count'=>0),
								'a_15'=>array('a_id'=>15,'name'=>'导航服务','a_count'=>0),
								'a_16'=>array('a_id'=>16,'name'=>'校园新闻动态','a_count'=>0),
								'a_17'=>array('a_id'=>17,'name'=>'其他','a_count'=>0),
						),
						'qsort'=>1,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'你认为直饮水APP存在哪些不足？(多选)',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'功能不够多','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'版面设计不够美观','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'进入时间比较长','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'计费不准','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'待机倒计时比较短','a_count'=>0),
								'a_6'=>array('a_id'=>6,'name'=>'体验感不够好','a_count'=>0),
								'a_7'=>array('a_id'=>7,'name'=>'支付方式不便利','a_count'=>0),
								'a_8'=>array('a_id'=>8,'name'=>'其他','a_count'=>0,'extra'=>1),
						),
						'qsort'=>1,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'根据你以往的使用经验，你认为直饮水APP对你的用处大吗？',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'非常有用','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'比较有用','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'一般','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'用处不大','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'根本没用','a_count'=>0),
						),
						'qsort'=>0,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'你不使用直饮水APP的原因？(多选)',
						'answer'=>array(
								'a_1'=>array('a_id'=>1,'name'=>'安装麻烦，占用内存','a_count'=>0),
								'a_2'=>array('a_id'=>2,'name'=>'使用起来不方便，软件有缺陷','a_count'=>0),
								'a_3'=>array('a_id'=>3,'name'=>'支付方式不便利','a_count'=>0),
								'a_4'=>array('a_id'=>4,'name'=>'提供服务较少，或不符合个人需要','a_count'=>0),
								'a_5'=>array('a_id'=>5,'name'=>'对软件不了解','a_count'=>0),
								'a_6'=>array('a_id'=>6,'name'=>'其他','a_count'=>0),
						),
						'qsort'=>1,
						'qlimit'=>0
				),
				array(
						'id'=>0,
						'title'=>'对于产品的见解及改善意见',
						'qsort'=>2,
						'qlimit'=>0
				),
				
		);
		
		//插入数据
		if ($insert){
			foreach ($data as $k=>$v){
				$id = $this->mongo_db_n->mid('qid', 'idlist');//取自增id
				$v['id']=$id;
				$this->mongo_db_n->toInsert('question',$v);
			}
			echo 'finish';
		}else{
			echo '非法操作';
		}
		
	}
	
}